from django.shortcuts import render
from django import http
from django.views import View
from django.utils.decorators import method_decorator
# from django.contrib.auth.mixins import LoginRequiredMixin


def my_decorator(func):
    def wrapper(request, *args, **kwargs):
        print('自定义装饰器被调用了')
        # print('请求路径%s' % request.path)
        return func(request, *args, **kwargs)
    return wrapper

# @my_decorator
def loginView(request):
    return http.HttpResponse('login')


# /loginview/
# 如果将method_decorator写在类名上时 有两个参数必须都传 (要被装饰的装饰器, name='要装饰类中的那个方法)
# 如果将method_decorator直接写在类中方法上面时,只用传第一个参数就可以, 因为它只装饰当前方法自己
# @method_decorator(my_decorator, name='dispatch')
class LoginView(View):
    """类视图"""
    # @my_decorator
    # @method_decorator(my_decorator)
    def get(self, request):
        # request.session['name'] = 'zhangsan'  # 设置session
        # name = request.session.get('name')  # 读写session
        # print(request.name)

        return http.HttpResponse('get')

    # @method_decorator(my_decorator)
    def post(self, request1):

        return http.HttpResponse('post')




# loginView()
# log = loginView
# log()

# handler = LoginView.get
# handler = getattr(self, 'get', self.http_method_not_allowed)
# handler = getattr(self, 'post', self.http_method_not_allowed)
# handler = LoginView.post
# handler = http.HttpResponseNotAllowed()
# # abc(loin, reqeust)
# handler()

